const { Collection } = require('@/models/community');
const { clearObject } = require('@/utils');

const getCollections = async(req, res) => {
  try {
    const { user_id, user_name, collection_type, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const collections = await Collection.getCollections(user_id, user_name, collection_type, offset, limit);
    res.status(200).send(collections);
  } catch (error) {
    res.status(500).json({ message: '查询收藏失败', error });
  }
}

const createCollection = async(req, res) => {
  try {
    const body = req.body;
    const params = clearObject(body);
    const collection = await Collection.createCollections(params);
    res.status(201).send(collection);
  } catch (error) {
    res.status(500).json({ message: '创建收藏失败', error });
  }
}
const deleteCollection = async(req, res) => {
  try {
    const { id } = req.params;
    const collection = await Collection.deleteCollection(id);
    res.status(200).send(collection);
  } catch (error) {
    res.status(500).json({ message: '删除收藏失败', error });
  }
}
const getStats = async(req, res) => {
  try {
    const { user_id } = req.query;
    const stats = await Collection.getStats(user_id);
    res.status(200).send(stats);
  } catch (error) {
    res.status(500).json({ message: '查询收藏统计失败', error });
  }
}
module.exports = {
  getCollections,
  createCollection,
  deleteCollection,
  getStats
}